﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace Krios
{
    public partial class Krios : Form
    {
        private string aURL = null;
        private string combopath = null;
        private string proxypath = null;
        private string outputFile = "logins.txt";
        private Stack comboList = Stack.Synchronized(new Stack());
        private  Queue proxyList = Queue.Synchronized(new Queue());
        private int doneCount = 0;
        private int initCLSize = 0;


        public Krios()
        {
            InitializeComponent();
        }

        private void openFileDialog1_FileOk_1(object sender, CancelEventArgs e)
        {

        }

        private void openFileDialog1_FileOk_2(object sender, CancelEventArgs e)
        {

        }

        private void openCombo_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            combopath = openFileDialog1.FileName;
            textBox1.Text = combopath;

        }
        private void openProxies_Click(object sender, EventArgs e)
        {
            openFileDialog2.ShowDialog();
            proxypath = openFileDialog2.FileName;
            textBox2.Text = proxypath;

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {
            if (Uri.IsWellFormedUriString(textBox3.Text, UriKind.Absolute))
            {
                urlstatuslab.Text = "Valid URL.";
                aURL = textBox3.Text;

            }
            else
            {
                urlstatuslab.Text = "Invalid URL.";
                aURL = null;
            }

        }

        private void butnAttack_Click(object sender, EventArgs e)
        {
            if (aURL != null && combopath != null && proxypath != null)
            {
                butnAttack.Enabled = false;
                butnAttack.Text = "Running..";
                // start attack.
                run_engine();
                butnAttack.Text = "Attack";
                butnAttack.Enabled = true;
            }
            else
            {
                MessageBox.Show("Missing parameters. Make sure everything is properly filled out.");
            }
        }

        private void run_engine()
        {
            // Successful auths are dumped to below StreamWriter.
            StreamWriter authStream = new StreamWriter(outputFile, true);
            // StreamReader to read combos from specified combolist text file.
            StreamReader comboStream = new StreamReader(combopath);
            // StreamReader to read proxies from specified proxylist text file.
            StreamReader proxyStream = new StreamReader(proxypath);
            String tmp_line;

            while ((tmp_line = proxyStream.ReadLine()) != null)
            {
                proxyList.Enqueue(tmp_line);
            }

            while ((tmp_line = comboStream.ReadLine()) != null)
            {
                comboList.Push(tmp_line);
            }
            // before we pop them all we save the size for progress GUI.
            initCLSize = comboList.Count;


            var allTasks = new List<Task>();
            while (comboList.Count != 0)
            {
                allTasks.Clear();
                if (proxyList.Count != 0)
                {
                    foreach (String combo in comboList)
                    {
                        String proxy = (String)proxyList.Dequeue();
                        // create the task 
                        Task task = new Task(() =>
                          {
                              attk(combo, proxy);
                          }, TaskCreationOptions.None);

                        // Add the tasks to a list
                        allTasks.Add(task);
                        task.Start();
                        if (proxyList.Count == 0) break;
                    }
                    // Wait until all tasks are completed.
                    Task.WaitAll(allTasks.ToArray());
                    // no working proxies exist anymore.
                    if (proxyList.Count == 0) break;
                }
            }

            // close all opened files
            authStream.Close();
            comboStream.Close();
            proxyStream.Close();
        }

        private void attk(String combo,String proxy)
        {
            MessageBox.Show(combo +"@"+proxy+" # "+ (doneCount / initCLSize) * 100);
            doneCount++;
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("KriosSharp\nWritten by Emanuel Y.Lindgren\nthegrus@gmail.com");
        }

    }
}
